package com.tsvetanv.depot.rejected.ideas;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;

/**
 * 
 * @author Tsvetan Vasilev
 * 
 *         The idea of this class is to support DAO implementation. After that
 *         this idea was replaced with "InMemory"-approach (because this FileIO
 *         implementation is too sluggish). The "InMemory"-approach use Map
 *         collection for storage(the best approach for educational purposes).
 * 
 *         This class have 2 methods. The first write a new line in a file (when
 *         add entity to the storage). The second method is more complicated -
 *         it delete a line from a file (when delete entity from the storage).
 * 
 */
public class IOUtils {
	private static final Logger logger = Logger.getLogger(IOUtils.class);

	private File file;
	private String charsetEncoding;

	// --- Constructor, Getter/Setter ------------------------------------------
	public IOUtils() {
	}

	public IOUtils(File file, String charsetEncoding) {
		this.file = file;
		this.charsetEncoding = charsetEncoding;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getCharsetEncoding() {
		return charsetEncoding;
	}

	public void setCharsetEncoding(String charsetEncoding) {
		this.charsetEncoding = charsetEncoding;
	}

	// --- Methods -------------------------------------------------------------

	public void writeObject(Object object) {
		PrintStream fileWriiter = null;
		try {
			BufferedOutputStream buffOutputStream = new BufferedOutputStream(
					new FileOutputStream(file, true));
			fileWriiter = new PrintStream(buffOutputStream, false,
					charsetEncoding);
			fileWriiter.println();
			fileWriiter.print(object.toString());
		} catch (FileNotFoundException e) {
			logger.error(" File not found", e);
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			logger.error("Encoding Exception during writing file", e);
		} finally {
			if (fileWriiter != null) {
				fileWriiter.close();
			}
		}
	}

	public void deleteObject(Object object) {
		File tempFile = new File(file.getAbsolutePath() + ".tmp");

		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

			String lineForDeletion = object.toString();
			String currentLine = null;
			while ((currentLine = reader.readLine()) != null) {
				if ((currentLine.trim()).equalsIgnoreCase(lineForDeletion)) {
					continue;
				}
				writer.write(currentLine);
				writer.newLine();
			}

			reader.close();
			writer.close();

			readWriteInFiles(tempFile, file);
		} catch (Exception e) {
			logger.error("Some Exception during delition", e);
		}
	}

	private static void readWriteInFiles(File inputFile, File outputFile) {
		try {

			FileInputStream fis = new FileInputStream(inputFile);
			FileOutputStream fos = new FileOutputStream(outputFile);
			int c;

			while ((c = fis.read()) != -1) {
				fos.write(c);
			}

			fis.close();
			fos.close();
		} catch (FileNotFoundException e) {
			System.err.println("FileStreamsTest: " + e);
		} catch (IOException e) {
			System.err.println("FileStreamsTest: " + e);
		}
	}
}
